package com.cpit.platform.web.controller;

import java.util.Date;
import java.util.List;

import com.cpit.platform.common.constant.LogConstant;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.utils.LogUtils;
import com.cpit.platform.dto.OperLog;
import com.cpit.platform.service.OperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.dto.Role;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.service.RoleService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/Role")
public class RoleController {
	private String moduleNmae = "角色：";
	private final String loggerName = this.getClass().getName();
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private OperLogService operLogService;

	@ApiOperation(value="查询角色列表", notes="通过条件查询系统中的角色信息" + 
	"支持分页查询，支持模糊查询。")
	@ResponseBody
	@PreAuthorize("hasAuthority('queryRole')")
	@RequestMapping(value="/queryRole", method = RequestMethod.GET)  
	public List<Role> queryRole(
			@ApiParam(value = "当前页数， 以1开始。") @RequestParam(value = "page", required = true, defaultValue = "1") int page, 
			@ApiParam(value = "当前页的内容长度。") @RequestParam(value = "size", required = true, defaultValue = "20") int size, 
			@ApiParam(value = "排序方式：asc为升序，desc为降序。") @RequestParam(value = "order", required = false, defaultValue = "asc") String order, 
			@ApiParam(value = "排序字段，与dto中声明的字段名称一致。") @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy, 
			@ApiParam(value = "模糊搜索的字符串，根据角色的名称字段进行模糊匹配。") @RequestParam(value = "pattern", required = false, defaultValue = "") String pattern) {
		return roleService.findByCondition(page, size, order, orderBy, pattern);
	}  
	
	@ApiOperation(value="查询角色列表", notes="通过条件查询系统中的角色信息" + 
			"支持分页查询，支持模糊查询。")
		    @ApiImplicitParams({
		            @ApiImplicitParam(name = "cond", value = "JqGrid的条件对象，由JqGrid根据js参数自动封装。", required = true, dataType = "TableDataQueryCondition")
		    })
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@PreAuthorize("hasAuthority('queryRole')")//有queryAuthority权限的才能访问  
	@RequestMapping(value="/queryRoleJqg", method = RequestMethod.GET)  
	public TableJsonData queryRoleJqg(TableDataQueryCondition cond) {
		TableJsonData tjd = roleService.findByConditionJqg(cond);
		return tjd;
	}  
	
	@ApiOperation(value="创建角色项目", notes="用户手工指定角色项目并创建，" + 
			"需要手动输入角色的详细信息，包括名称、描述，以及对应的权限信息。")
		     @ApiImplicitParam(name = "data", value = "角色描述对象。", required = true, dataType = "Role")
	@ResponseBody
	@PreAuthorize("hasAuthority('createRole')")//有createRole角色的才能访问  
	@RequestMapping(value="/createRole", method = RequestMethod.POST)  
	public Result<Role> createRole(@RequestBody Role data) {
		try{
			roleService.create(data);
			writeLog(LogConstant.OPER_CREATE, Result.CODE_SUCCESS,data.getName());
			return new Result<Role>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			LogOperator.err(loggerName, "createRole error", e);
			writeLog(LogConstant.OPER_CREATE, Result.CODE_FAILED,e.getMessage());
			return new Result<Role>(Result.CODE_FAILED, e.getMessage(), data);
		}
	}  
	
	@ApiOperation(value="修改角色项目", notes="用户手工指定角色项目并修改，" + 
			"需要手动输入角色的详细信息，包括名称、描述以及在子系统中的模块ID，鉴权标识等等。")
		     @ApiImplicitParam(name = "data", value = "角色描述对象。", required = true, dataType = "Role")
	@ResponseBody
	@PreAuthorize("hasAuthority('modifyRole')")
	@RequestMapping(value="/modifyRole", method = RequestMethod.POST)  
	public Result<Role> modifyRole(@RequestBody Role data) {
		try{
			roleService.modify(data);
			writeLog(LogConstant.OPER_MODIFY, Result.CODE_SUCCESS,data.getName());
			return new Result<Role>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			LogOperator.err(loggerName, "modifyRole error", e);
			writeLog(LogConstant.OPER_MODIFY, Result.CODE_FAILED,e.getMessage());
			return new Result<Role>(Result.CODE_FAILED, e.getMessage(), data);
		}
	}

	@ApiOperation(value="删除角色项目", notes="在系统中删除已经存在的角色项目，" + 
			"如果该角色被角色引用，则不允许删除。")
		     @ApiImplicitParam(name = "data", value = "角色描述对象。", required = true, dataType = "Role")
	@ResponseBody
	@PreAuthorize("hasAuthority('deleteRole')")
	@RequestMapping(value="/deleteRole", method = RequestMethod.DELETE)  
	public Result<Role> deleteRole(@RequestBody Role data) {
		try{
			roleService.delete(data);
			writeLog(LogConstant.OPER_DELETE, Result.CODE_SUCCESS,data.getName());
			return new Result<Role>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			writeLog(LogConstant.OPER_DELETE, Result.CODE_FAILED,e.getMessage());
			return new Result<Role>(Result.CODE_FAILED, "", data);
		}
	}
	private  void  writeLog(String op,String result, String detail){
		OperLog operLog = LogUtils.createOperLog(LogConstant.MODULE_CONFIG,op,result,moduleNmae + detail);
		operLogService.writeLog(operLog);
	}
}
